package com.chen10.cattle.common.protocal;

import com.chen10.cattle.common.enums.response.ErrorEnum;
import com.chen10.cattle.common.enums.response.ResponseCodeEnum;
import com.chen10.cattle.common.enums.response.SuccessMsgEnum;

public class ResponseResult {

    protected static final String STATUS_SUCCESS = "success";
    protected static final String STATUS_ERROR = "error";

    private static <T> ResponseVO<T> buildResponse(String status, Integer code, String message, T data) {
        ResponseVO<T> responseVO = new ResponseVO<>();
        responseVO.setStatus(status);
        responseVO.setCode(code);
        responseVO.setMessage(message);
        responseVO.setData(data);
        return responseVO;
    }

    // 成功响应
    public static <T> ResponseVO<T> success() {
        return buildResponse(STATUS_SUCCESS, ResponseCodeEnum.REQUEST_SUCCESS.getCode(), ResponseCodeEnum.REQUEST_SUCCESS.getMsg(), null);
    }

    public static <T> ResponseVO<T> success(String msg) {
        return buildResponse(STATUS_SUCCESS, ResponseCodeEnum.REQUEST_SUCCESS.getCode(), msg, null);
    }

    public static <T> ResponseVO<T> success(SuccessMsgEnum successMsgEnum) {
        return buildResponse(STATUS_SUCCESS, ResponseCodeEnum.REQUEST_SUCCESS.getCode(), successMsgEnum.getMsg(), null);
    }

    public static <T> ResponseVO<T> success(T t) {
        return buildResponse(STATUS_SUCCESS, ResponseCodeEnum.REQUEST_SUCCESS.getCode(), ResponseCodeEnum.REQUEST_SUCCESS.getMsg(), t);
    }

    public static <T> ResponseVO<T> success(String msg, T t) {
        return buildResponse(STATUS_SUCCESS, ResponseCodeEnum.REQUEST_SUCCESS.getCode(), msg, t);
    }

    // 错误响应
    public static <T> ResponseVO<T> error(String msg) {
        return buildResponse(STATUS_ERROR, null, msg, null);
    }

    public static <T> ResponseVO<T> error(Integer code, String msg) {
        return buildResponse(STATUS_ERROR, code, msg, null);
    }

    public static <T> ResponseVO<T> error(ResponseCodeEnum enums) {
        return buildResponse(STATUS_ERROR, enums.getCode(), enums.getMsg(), null);
    }

    public static <T> ResponseVO<T> error(ResponseCodeEnum enums, String errorMsg) {
        return buildResponse(STATUS_ERROR, enums.getCode(), errorMsg, null);
    }

    public static <T> ResponseVO<T> error(ErrorEnum errorEnum) {
        return buildResponse(STATUS_ERROR, errorEnum.getCode(), errorEnum.getMessage(), null);
    }
}
